import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

import Home from "@/pages/Home"
import Search from "@/pages/Search"
import Login from "@/pages/Login"
import Register from "@/pages/Register"

const originPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, resolve, reject) {
    if (resolve && reject) {
        return originPush.call(this, location, resolve, reject)
    } else {
        return originPush.call(this, location).catch(() => {})
    }
}

const originReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location, resolve, reject) {
    if (resolve && reject) {
        return originReplace.call(this, location, resolve, reject)
    } else {
        return originReplace.call(this, location).catch(() => {})
    }
}




export default new VueRouter({
    routes: [
        {
            path: "/home",
            component: Home
        },
        {
            path: "/search/:keyword?", // 加的?代表占位的keyword可传可不传
            component: Search,
            name: "search",
            // props: true, // 第一种,params能接收到,query接收不到
            // 第二种 传额外数据使用
            // props: {
            //     username: "zhangsan"
            // },
            props: (route) => {
                return {
                    keyword: route.params.keyword,
                    keyword1: route.query.keyword1,
                    username: "lisi"
                }
            }
        },
        {
            path: "/login",
            component: Login,
            meta: { // meta 可以设置一些自己的属性
                isHidden: true
            }
        },
        {
            path: "/register",
            component: Register,
            meta: { // meta 可以设置一些自己的属性
                isHidden: true
            }
        },
        {
            path: "/",
            redirect: "/home"
        },
    ]
})
